package com.ruoyi.project.user.kkuserInfo.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.poem.poem.domain.Poem;
import com.ruoyi.project.poem.poemuser.domain.PoemUser;
import com.ruoyi.project.poem.poemuser.service.IPoemUserService;
import com.ruoyi.project.user.cancelaccount.domain.CancelAccount;
import com.ruoyi.project.user.cancelaccount.service.ICancelAccountService;
import com.ruoyi.project.user.dictKkuserInfo.domain.DictKkuserInfo;
import com.ruoyi.project.user.dictKkuserInfo.mapper.DictKkuserInfoMapper;
import com.ruoyi.project.user.dictKkuserInfo.service.IDictKkuserInfoService;
import com.ruoyi.project.user.kkidmapper.domain.KkidMapper;
import com.ruoyi.project.user.kkidmapper.mapper.KkidMapperMapper;
import com.ruoyi.project.user.kkidmapper.service.IKkidMapperService;
import com.ruoyi.project.user.kknumber.domain.KkNumber;
import com.ruoyi.project.user.kknumber.mapper.KkNumberMapper;
import com.ruoyi.project.user.kknumber.service.IKkNumberService;
import com.ruoyi.project.user.kkuser.domain.KKUser;
import com.ruoyi.project.user.kkuser.service.IKKUserService;
import com.ruoyi.project.user.kkuserInfo.domain.AppUserInfo;
import com.ruoyi.project.user.usermapper.domain.UserMapper;
import com.ruoyi.project.user.usermapper.service.IUserMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.user.kkuserInfo.mapper.KkuserInfoMapper;
import com.ruoyi.project.user.kkuserInfo.domain.KkuserInfo;
import com.ruoyi.project.user.kkuserInfo.service.IKkuserInfoService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 用户信息(按kkuid分区)Service业务层处理
 *
 * @author shixiangwen
 * @date 2024-07-24
 */
@Service
@DataSource(DataSourceType.ACCOUNT)
public class KkuserInfoServiceImpl implements IKkuserInfoService {
    @Autowired
    private KkuserInfoMapper kkuserInfoMapper;

    @Autowired
    private IDictKkuserInfoService dictKkuserInfoService;

    @Autowired
    private IKkidMapperService kkidMapperService;

    @Autowired
    private IKkNumberService kkNumberService;

    @Autowired
    private IUserMapperService userMapperService;

    @Autowired
    private IKKUserService kkUserService;

    @Autowired
    private IPoemUserService poemUserService;

    @Autowired
    private ICancelAccountService cancelAccountService;

    /**
     * 查询用户信息(按kkuid分区)
     *
     * @param kkuid 用户信息(按kkuid分区)ID
     * @return 用户信息(按kkuid分区)
     */
    @Override
    public KkuserInfo selectKkuserInfoById(Long kkuid) {
        return kkuserInfoMapper.selectKkuserInfoById(kkuid);
    }

    /**
     * 查询用户信息(按kkuid分区)列表
     *
     * @param kkuserInfo 用户信息(按kkuid分区)
     * @return 用户信息(按kkuid分区)
     */
    @Override
    public List<KkuserInfo> selectKkuserInfoList(KkuserInfo kkuserInfo) {
        return kkuserInfoMapper.selectKkuserInfoList(kkuserInfo);
    }

    @Override
    public int reuse(String appId, String userId) {
        CancelAccount cancelAccount = cancelAccountService.queryCancelAccount(appId, userId);
        if (cancelAccount != null) {
            return cancelAccountService.deleteCancelAccountById(cancelAccount.getId());
        }
        return 0;
    }

    @Override
    public List<AppUserInfo> selectAppUserInfoList(KkuserInfo kkuserInfo) {
        List<AppUserInfo> users = new ArrayList<>();
        if (kkuserInfo.getPhone() == null || kkuserInfo.getPhone() <= 0) {
            return users;
        }
        KkuserInfo dbkkuserInfo = kkuserInfoMapper.selectKkuserInfoByPhone(kkuserInfo.getPhone());
        if (dbkkuserInfo != null) {
            long kkuid = dbkkuserInfo.getKkuid();
            //字典
            {
                DictKkuserInfo dictKkuserInfoQuery = new DictKkuserInfo();
                dictKkuserInfoQuery.setType(4);
                dictKkuserInfoQuery.setOpenid(String.valueOf(kkuid));
                List<DictKkuserInfo> dictKkuserInfos = dictKkuserInfoService.selectDictKkuserInfoList(dictKkuserInfoQuery);
                if (!dictKkuserInfos.isEmpty()) {
                    DictKkuserInfo dictKkuserInfo = dictKkuserInfos.get(0);
                    AppUserInfo appUserInfo = new AppUserInfo();
                    appUserInfo.setAppId("kkdict");
                    appUserInfo.setNickname(dictKkuserInfo.getNickname());
                    appUserInfo.setCreatedTime(dictKkuserInfo.getRegistertime());
                    appUserInfo.setUserId(String.valueOf(dictKkuserInfo.getKkuid()));
                    KkidMapper kkidMapperQuery = new KkidMapper();
                    kkidMapperQuery.setAppId("kkdict");
                    kkidMapperQuery.setUid(String.valueOf(dictKkuserInfo.getKkuid()));
                    KkidMapper kkidMapper = kkidMapperService.queryKkidMapper(kkidMapperQuery);
                    if (kkidMapper != null) {
                        appUserInfo.setKkid(kkidMapper.getKkid());
                    }
                    users.add(appUserInfo);

                }
            }

            //古诗词典
            {
                PoemUser poemUserQuery = new PoemUser();
                poemUserQuery.setThirdType("kk");
                poemUserQuery.setThirdUserId(String.valueOf(dbkkuserInfo.getKkuid()));
                List<PoemUser> poemUsers = poemUserService.selectPoemUserList(poemUserQuery);
                if (!poemUsers.isEmpty()) {
                    PoemUser poemUser = poemUsers.get(0);
                    AppUserInfo appUserInfo = new AppUserInfo();
                    appUserInfo.setAppId("kkpoem");
                    appUserInfo.setUserId(poemUser.getUserId());
                    appUserInfo.setNickname(poemUser.getNickname());
                    appUserInfo.setCreatedTime(poemUser.getCreatedTime());
                    KkidMapper kkidMapperQuery = new KkidMapper();
                    kkidMapperQuery.setAppId("kkpoem");
                    kkidMapperQuery.setUid(poemUser.getUserId());
                    KkidMapper kkidMapper = kkidMapperService.queryKkidMapper(kkidMapperQuery);
                    if (kkidMapper != null) {
                        appUserInfo.setKkid(kkidMapper.getKkid());
                    }
                    users.add(appUserInfo);

                }
            }

            for (String appId : new String[]{"kkdictios", "dictkx", "wyw", "dictkxios", "cidianhy", "dictsw"}) {
                UserMapper userMapper = userMapperService.queryUserMapper(appId, String.valueOf(dbkkuserInfo.getKkuid()));
                if (userMapper != null) {
                    AppUserInfo appUserInfo = new AppUserInfo();
                    appUserInfo.setAppId(appId);
                    KKUser user = kkUserService.queryKKUser(appId, userMapper.getKkid());
                    if (user != null) {
                        appUserInfo.setNickname(user.getNickname());
                    }
                    appUserInfo.setCreatedTime(userMapper.getCreatedTime());
                    appUserInfo.setKkid(userMapper.getKkid());
                    appUserInfo.setUserId(String.valueOf(userMapper.getKkid()));
                    users.add(appUserInfo);
                }
            }

            for (AppUserInfo appUserInfo : users) {
                if (appUserInfo.getKkid() > 0) {
                    KkNumber kkNumberQuery = new KkNumber();
                    kkNumberQuery.setAppId(appUserInfo.getAppId());
                    kkNumberQuery.setKkid(appUserInfo.getKkid());
                    List<KkNumber> kkNumbers = kkNumberService.selectKkNumberList(kkNumberQuery);
                    if (!kkNumbers.isEmpty()) {
                        appUserInfo.setKkNumber(kkNumbers.get(0).getNo());
                    }
                }
                if (!StringUtils.isEmpty(appUserInfo.getUserId())) {
                    CancelAccount cancelAccount = cancelAccountService.queryCancelAccount(appUserInfo.getAppId(), appUserInfo.getUserId());
                    if (cancelAccount != null) {
                        appUserInfo.setCancel(1);
                    }
                }
            }
        }
        return users;
    }

    /**
     * 新增用户信息(按kkuid分区)
     *
     * @param kkuserInfo 用户信息(按kkuid分区)
     * @return 结果
     */
    @Override
    public int insertKkuserInfo(KkuserInfo kkuserInfo) {
        return kkuserInfoMapper.insertKkuserInfo(kkuserInfo);
    }

    /**
     * 修改用户信息(按kkuid分区)
     *
     * @param kkuserInfo 用户信息(按kkuid分区)
     * @return 结果
     */
    @Override
    public int updateKkuserInfo(KkuserInfo kkuserInfo) {
        return kkuserInfoMapper.updateKkuserInfo(kkuserInfo);
    }

    /**
     * 删除用户信息(按kkuid分区)对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteKkuserInfoByIds(String ids) {
        return kkuserInfoMapper.deleteKkuserInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户信息(按kkuid分区)信息
     *
     * @param kkuid 用户信息(按kkuid分区)ID
     * @return 结果
     */
    @Override
    public int deleteKkuserInfoById(Long kkuid) {
        return kkuserInfoMapper.deleteKkuserInfoById(kkuid);
    }
}
